//
//  PopularityViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "PopularityViewController.h"
#import "ChoiceView.h"
#import "PopularityListViewController.h"
#import "RuleRankingViewController.h"

@interface PopularityViewController ()<UIScrollViewDelegate>

@property(nonatomic,weak) UIScrollView *scrollView;

@property(nonatomic,weak) ChoiceView *choiceView;

@end

@implementation PopularityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleStr;
    if ([self.title isEqualToString:@"人气榜"]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"排行规则" target:self selector:@selector(PopularityRule)];
    }
    [self creatScroll];
}
#pragma mark - 排行规则
- (void)PopularityRule{
    RuleRankingViewController *ruleRankingVC = [[RuleRankingViewController alloc]init];
    [self.navigationController pushViewController:ruleRankingVC animated:YES];
}
- (void)creatScroll{
    ChoiceView *choiceview=[[ChoiceView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 41) andtitleArray:self.titleArray];
    [self.view addSubview:choiceview];
    self.choiceView=choiceview;
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(choiceview.frame), kScreenW, kScreenH-CGRectGetMaxY(choiceview.frame) + 20)];
    scrollView.pagingEnabled=YES;
    scrollView.backgroundColor=[UIColor whiteColor];
    scrollView.delegate=self;
    scrollView.contentSize=CGSizeMake(CGRectGetWidth(scrollView.frame)*3, 0);
    [self.view addSubview:scrollView];
    self.scrollView=scrollView;
    
    choiceview.clickbuttonblock=^(NSInteger index){
        CGPoint point=self.scrollView.contentOffset;
        self.scrollView.contentOffset=CGPointMake(index*CGRectGetWidth(self.scrollView.frame), point.y);
    };
    TableViewPresentViewController *dayVC =  [[NSClassFromString(self.vcStr) alloc]init];
    dayVC.popularityListType = KPopularityListDayType;
    dayVC.homeworkType = KCurrentHomeWorkType;
    [dayVC willMoveToParentViewController:self];
    [self addChildViewController:dayVC];
    [dayVC didMoveToParentViewController:self];
    dayVC.view.frame=self.view.frame;
    [scrollView addSubview:dayVC.view];
    
    
    TableViewPresentViewController *monthVC =  [[NSClassFromString(self.vcStr) alloc]init];
    monthVC.popularityListType = KPopularityListWeekType;
    monthVC.homeworkType = KBackHomeWorkType;
    [monthVC willMoveToParentViewController:self];
    CGRect rect=self.view.frame;
    rect.origin.x=kScreenW;
    monthVC.view.frame=rect;
    [self addChildViewController:monthVC];
    [monthVC didMoveToParentViewController:self];
    [scrollView addSubview:monthVC.view];
    
    TableViewPresentViewController *yearVC =  [[NSClassFromString(self.vcStr) alloc]init];
    yearVC.popularityListType = KPopularityListMonthType;
    [yearVC willMoveToParentViewController:self];
    CGRect yearRect=self.view.frame;
    yearRect.origin.x=kScreenW * 2;
    yearVC.view.frame=yearRect;
    [self addChildViewController:yearVC];
    [yearVC didMoveToParentViewController:self];
    [scrollView addSubview:yearVC.view];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index=(NSInteger)(scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame)+0.5);
    [self.choiceView clickbuttonAtIndex:index];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
